﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using P2PWeb.Models;

namespace P2PWeb.Common
{
    public class Menu
    {
        public Menu(string controllerName)
        {
            this.ControllerName = controllerName;

            if (string.IsNullOrWhiteSpace(ControllerName))
            {
                throw new ArgumentNullException();
            }
            Type type = Type.GetType(string.Format("P2PWeb.Controllers.{0}Controller", ControllerName));
            if (type == null)
            {
                throw new ArgumentNullException();
            }
            if (AccountAuthorizeHelper.AccountAuthorizes.ContainsKey(type))
                AccountAuthorize = AccountAuthorizeHelper.AccountAuthorizes[type];
        }

        public string Name { get; set; }

        public string ControllerName { get; private set; }

        public string ActionName { get; set; }

        public AccountAuthorize AccountAuthorize
        {
            get;
            set;
        }
    }

    public class MenuCategory
    {
        public String Name { get; set; }

        public string Title { get; set; }

        public Menu[] Menus { get; set; }
    }

    public class MenuHelper
    {
        public static MenuCategory[] MenuCategorys = new MenuCategory[]
        {
            new MenuCategory()
            {
                Name="my_message",
                Title="我的资料",
                 Menus=new []
                 {
                     new Menu("Account")
                     {
                          ActionName="Index",
                            Name="用户信息"
                     },
                 },
            },


            new MenuCategory()
            {
                Name="i_want_borrow",
                Title="我要借款",
                 Menus=new []
                 {
                     new Menu("BorrowStep1")
                     {
                          ActionName="Index",
                            Name="我要借款"
                     },
                                          new Menu("MyBorrows")
                     {
                          ActionName="Index",
                            Name="我已借款单"
                     },
                 },
            },


            new MenuCategory()
            {
                Name="i_want_borrow",
                Title="我要出借",
                 Menus=new []
                 {
                     new Menu("MyLends")
                     {
                          ActionName="Index",
                            Name="我已出借单"
                     },
                 },
            },

             new MenuCategory()
            {
                Name="my_message",
                Title="代理商",
                 Menus=new []
                 {
                     new Menu("Agent")
                     {
                          ActionName="Index",
                            Name="代理商管理"
                     },
                     new Menu("Agent")
                     {
                         ActionName="BindAgent",
                         Name="用户代理商"
                     },
                 },
            },

            //new MenuCategory()
            //{
            //    Name="money_manage",
            //    Title="资金管理",
            //     Menus=new []
            //     {
            //         new Menu("FundingDetails")
            //         {
            //              ActionName="Index",
            //                Name="我的资金明细"
            //         },
            //     },
            //},

            new MenuCategory()
            {
                Name="my_message",
                Title="审核",
                 Menus=new []
                 {
                     new Menu("BorrowsManager")
                     {
                          ActionName="Index",
                            Name="借款单管理"
                     },
                                         new Menu("AccountManager")
                     {
                          ActionName="Index",
                            Name="用户管理"
                     },
                 },

            },
        };


        public static string WhetherControllerBelongMenu(string menuName, ViewContext context)
        {
            string controllerName = context.RouteData.Values["controller"] + "";
            return controllerName == menuName ? "cur" : string.Empty;
        }

        public static string WhetherControllerBelongMenuCategory(string categroyName, ViewContext context)
        {
            string controllerName = context.RouteData.Values["controller"] + "";
            MenuCategory category = MenuCategorys.First(mc => mc.Menus.Any(m => string.Compare(m.Name, controllerName, true) == 0));

            return category.Name == categroyName ? string.Empty : "vh";
        }
    }
}